package com.ybkj.chat.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.ybkj.chat.R;

import butterknife.OnClick;

/**
 * @author ywh
 * @date 2019/2/18
 * 排序查找弹窗
 */

public class PoSortnDialog extends PopupWindow implements View.OnClickListener {


    private LayoutInflater mInflater;
    private View mView;


    private OnItemClickListener onItemClickListener;

    private Context mContext;
    private Activity mActivity;

    public PoSortnDialog(Context context) {
        super(context);
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        init();
        setPopwindow();
    }

    private void init() {
        mView = mInflater.inflate(R.layout.pop_sort_dialog, null);

    }

    private void setPopwindow() {
        this.setContentView(mView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        // this.setAnimationStyle(R.style.ppw_select_picture);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setFocusable(true);
        this.setOutsideTouchable(true);

//        mView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                int height = v.findViewById(R.id.ll_pop_layout).getTop();
//                int y = (int) event.getY();
//                if (event.getAction() == MotionEvent.ACTION_UP){
//                    if (y < height){
//                        dismiss();
//                    }
//                }
//                return true;
//            }
//        });

        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mActivity != null) {
                    setBackgroundAlpha(mActivity, 1f);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(v);
        }
    }

    /**
     * 设置页面的透明度
     *
     * @param bgAlpha 1表示不透明
     */
    public void setBackgroundAlpha(Activity activity, float bgAlpha) {
        this.mActivity = activity;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        dismiss();
    }


    public interface OnItemClickListener {
        void onItemClick(View v);
    }

}
